package own.stu.jobgib.playown.util.ftp;

import org.apache.commons.lang3.StringUtils;
import sun.net.ftp.FtpProtocolException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * ftp 下载 指令
 */
public class FtpDownloadCommand extends AbstractFtpCommand<FtpDownloadParameter, InputStream> {

    @Override
    protected InputStream operate(FtpDownloadParameter parameter) {
        InputStream inputStream = null;
        String downLoadUrl = parameter.getRemotePath() + parameter.getFtpFileName();
        try {
            // 获取ftp上的文件 
            inputStream = parameter.getFtpClient().getFileStream(downLoadUrl);
        } catch (FtpProtocolException e) {
            log.error("FtpProtocolException, ", e);
        } catch (FileNotFoundException e) {
            log.error("FileNotFoundException, ", e);
        } catch (IOException e) {
            log.error("IOException, ", e);
        }
        return inputStream;
    }

    @Override
    protected void checkParameter(FtpDownloadParameter parameter) {

        if (StringUtils.isEmpty(parameter.remotePath) || StringUtils.isEmpty(parameter.getFtpFileName())) {
            throw new RuntimeException("File name empty!");
        }
    }

    @Override
    public void setParameter(FtpDownloadParameter parameter) {
        this.parameter = parameter;
    }
}
